package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sun
 */
@Service
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Resource
    private BaseCategory1Mapper baseCategory1Mapper;

    @Resource
    private BaseCategory2Mapper baseCategory2Mapper;

    @Resource
    private BaseCategory3Mapper baseCategory3Mapper;

    @Resource
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Resource
    private BaseAttributeMapper baseAttributeMapper;

    /**
     * 获取分类列表
     *
     * @return JSON形式封装返回的数据
     */
    @Override
    public List<JSONObject> getBaseCategoryList() {
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        // 根据一级分类的分类map
        Map<Long, List<BaseCategoryView>> category1s = baseCategoryViews.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        return category1s.entrySet().stream()
                .map(category1 -> {
                    JSONObject category1Json = new JSONObject();
                    category1Json.put("categoryId", category1.getKey());
                    List<BaseCategoryView> category1List = category1.getValue();
                    category1Json.put("categoryName",
                            category1List.getFirst()
                                    .getCategory1Name()
                    );
                    // 根据二级分类的分类map
                    Map<Long, List<BaseCategoryView>> category2s = category1List.stream()
                            .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
                    List<JSONObject> category2Jsons = category2s.entrySet().stream().map(category2 -> {
                        JSONObject category2Json = new JSONObject();
                        category2Json.put("categoryId", category2.getKey());
                        category2Json.put("categoryName", category2.getValue().getFirst().getCategory2Name());
                        List<JSONObject> category3Jsons = category2.getValue().stream().map(category3 -> {
                            JSONObject category3Json = new JSONObject();
                            category3Json.put("categoryId", category3.getCategory3Id());
                            category3Json.put("categoryName", category3.getCategory3Name());
                            return category3Json;
                        }).toList();
                        category2Json.put("categoryChild", category3Jsons);
                        return category2Json;
                    }).toList();
                    category1Json.put("categoryChild", category2Jsons);
                    return category1Json;
                })
                .toList();
    }

    /**
     * 根据一级分类id查询标签信息
     *
     * @param category1Id 一级分类id
     * @return 标签信息
     */
    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.findAttribute(category1Id);
    }

    /**
     * 根据三级分类id查询分类信息
     *
     * @param category3Id 三级分类id
     * @return 分类信息
     */
    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    /**
     * 根据一级分类id查询排名前七的三级分类
     *
     * @param category1Id 一级分类id
     * @return 一级分类下排名前七的三级分类
     */
    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        return baseCategory3Mapper.selectTopBaseCategory3(category1Id);
    }

    /**
     * 根据一级分类id查询二级和三级分类信息
     *
     * @param category1Id 一级分类id
     * @return 二级和三级分类信息
     */
    @Override
    public JSONObject getBaseCategoryListById(Long category1Id) {

        List<BaseCategoryView> category1s = baseCategoryViewMapper.selectList(
                new LambdaQueryWrapper<BaseCategoryView>()
                        .eq(BaseCategoryView::getCategory1Id, category1Id)
        );
        if (!category1s.isEmpty()) {
            JSONObject jsonpObject = new JSONObject();
            String category1Name = category1s.getFirst().getCategory1Name();
            jsonpObject.put("categoryId", category1Id);
            jsonpObject.put("categoryName", category1Name);
            Map<Long, List<BaseCategoryView>> category2Map = category1s.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<JSONObject> category2Jsons = category2Map.entrySet().stream().map(
                    category2Entry -> {
                        JSONObject category2Json = new JSONObject();
                        Long category2Id = category2Entry.getKey();
                        List<BaseCategoryView> category2s = category2Entry.getValue();
                        category2Json.put("categoryId", category2Id);
                        category2Json.put("categoryName", category2s.getFirst().getCategory2Name());
                        List<JSONObject> category3Jsons = category2s.stream().map(category3 -> {
                            JSONObject category3Json = new JSONObject();
                            category3Json.put("categoryId", category3.getCategory3Id());
                            category3Json.put("categoryName", category3.getCategory3Name());
                            return category3Json;
                        }).toList();
                        category2Json.put("categoryChild", category3Jsons);
                        return category2Json;
                    }
            ).toList();
            jsonpObject.put("categoryChild", category2Jsons);
            return jsonpObject;
        }
        return null;
    }


}
